//
//  MyCollectionViewFlowLayout.m
//  test
//
//  Created by wenchao ren on 16/9/28.
//  Copyright © 2016年 wenchao ren. All rights reserved.
//

#import "MyCollectionViewFlowLayout.h"
#import "MyCollectionReusableViewDecoration.h"

@implementation MyCollectionViewFlowLayout

-(void)awakeFromNib {
    [super awakeFromNib];
    [self setupLayout];
}

- (id)init {
    if (self = [super init]) {
        [self setupLayout];
    }
    return self;
}

- (void)setupLayout {
    self.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.minimumInteritemSpacing = 0.0;
    self.minimumLineSpacing = 0.0;
    self.headerReferenceSize = CGSizeMake(100, 20);
    self.footerReferenceSize = CGSizeMake(20, 20);
    
    [self registerClass:[MyCollectionReusableViewDecoration class] forDecorationViewOfKind:@"MyCollectionReusableViewDecoration"];
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)path
{
    UICollectionViewLayoutAttributes *attribute =[UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:path.row inSection:path.section]];
    attribute.frame = CGRectMake(path.row%3*100, path.row%4*100+path.section*400, 50, 50);
    return attribute;
}

//Decoration View的布局。
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString*)decorationViewKind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewLayoutAttributes* att = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:decorationViewKind withIndexPath:indexPath];
    att.frame=CGRectMake(0, 400*indexPath.section, 30, 30);
    att.zIndex=-1;
    
    return att;
    
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind
                                                                     atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionViewLayoutAttributes *sectionHeaderAttribute =[UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section]];
        sectionHeaderAttribute.frame = CGRectMake(0, indexPath.section*400, 100, 20);
        return sectionHeaderAttribute;
    }
    else if (kind == UICollectionElementKindSectionFooter){
        UICollectionViewLayoutAttributes *sectionFooterAttribute =[UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter withIndexPath:[NSIndexPath indexPathForItem:indexPath.row inSection:indexPath.section]];
        sectionFooterAttribute.frame = CGRectMake(0, 400*(1+indexPath.section), 100, 20);
//        sectionFooterAttribute.zIndex = 100;
        return sectionFooterAttribute;
    }
    return nil;
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    NSMutableArray* attributes = [NSMutableArray array];
    //把Decoration View的布局加入可见区域布局。
    for (int y=0; y<2; y++) {
        NSIndexPath* indexPath = [NSIndexPath indexPathForItem:0 inSection:y];
        [attributes addObject:[self layoutAttributesForDecorationViewOfKind:@"MyCollectionReusableViewDecoration"atIndexPath:indexPath]];
    }
    
    for (int i=0; i<2; ++i) {
        UICollectionViewLayoutAttributes *sectionHeaderAttribute =[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:i]];
        [attributes addObject:sectionHeaderAttribute];
    }
    
    for (int i=0; i<2; ++i) {
        UICollectionViewLayoutAttributes *sectionHeaderAttribute =[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionFooter atIndexPath:[NSIndexPath indexPathForItem:0 inSection:i]];
        [attributes addObject:sectionHeaderAttribute];
    }
    
    for (NSInteger i=0 ; i < 2; i++) {
        for (NSInteger t=0; t<12; t++) {
            [attributes addObject:[self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:t inSection:i]]];
        }
        
    }
    
    return attributes;
}

- (CGSize)collectionViewContentSize
{
    return CGSizeMake(self.collectionView.frame.size.width, 800);
}


@end
